//+------------------------------------------------------------------+
//|                                              TradeSignal.mqh |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024"
#property link      ""
#property version   "1.00"
#property strict

// 信号类型枚举
enum ENUM_SIGNAL_TYPE {
    SIGNAL_BUY,          // 买入信号
    SIGNAL_SELL,         // 卖出信号
    SIGNAL_CLOSE_BUY,    // 平多信号
    SIGNAL_CLOSE_SELL,   // 平空信号
    SIGNAL_NONE          // 无信号
};

// 信号来源枚举
enum ENUM_SIGNAL_SOURCE {
    SOURCE_MA,           // 移动平均线
    SOURCE_MACD,         // MACD
    SOURCE_RSI,          // RSI
    SOURCE_BB,           // 布林带
    SOURCE_ADX,          // ADX
    SOURCE_VOLUME,       // 成交量
    SOURCE_CUSTOM        // 自定义信号
};

// 信号结构
struct Signal {
    ENUM_SIGNAL_TYPE type;       // 信号类型
    ENUM_SIGNAL_SOURCE source;   // 信号来源
    double strength;             // 信号强度
    datetime time;               // 信号时间
    double price;                // 信号价格
    bool isActive;               // 是否活跃
};

// 交易信号生成类
class CTradeSignal {
private:
    Signal m_signals[];          // 信号数组
    int m_signalCount;           // 信号计数
    
    // 指标句柄
    int m_maHandle;              // MA句柄
    int m_macdHandle;            // MACD句柄
    int m_rsiHandle;             // RSI句柄
    int m_bbHandle;              // 布林带句柄
    int m_adxHandle;             // ADX句柄
    
    // 信号参数
    int m_maPeriod;              // MA周期
    int m_macdFastPeriod;        // MACD快线周期
    int m_macdSlowPeriod;        // MACD慢线周期
    int m_macdSignalPeriod;      // MACD信号周期
    int m_rsiPeriod;             // RSI周期
    int m_bbPeriod;              // 布林带周期
    int m_adxPeriod;             // ADX周期
    
    // 信号阈值
    double m_rsiOverbought;      // RSI超买阈值
    double m_rsiOversold;        // RSI超卖阈值
    double m_adxThreshold;       // ADX阈值
    double m_volumeThreshold;    // 成交量阈值
    
public:
    // 构造函数
    CTradeSignal() {
        m_signalCount = 0;
        
        // 默认参数
        m_maPeriod = 20;
        m_macdFastPeriod = 12;
        m_macdSlowPeriod = 26;
        m_macdSignalPeriod = 9;
        m_rsiPeriod = 14;
        m_bbPeriod = 20;
        m_adxPeriod = 14;
        
        m_rsiOverbought = 70.0;
        m_rsiOversold = 30.0;
        m_adxThreshold = 25.0;
        m_volumeThreshold = 1.5;
    }
    
    // 初始化
    bool Initialize() {
        // 创建指标句柄
        m_maHandle = iMA(Symbol(), PERIOD_CURRENT, m_maPeriod, 0, MODE_SMA, PRICE_CLOSE);
        if(m_maHandle == INVALID_HANDLE) return false;
        
        m_macdHandle = iMACD(Symbol(), PERIOD_CURRENT, m_macdFastPeriod, m_macdSlowPeriod, m_macdSignalPeriod, PRICE_CLOSE);
        if(m_macdHandle == INVALID_HANDLE) return false;
        
        m_rsiHandle = iRSI(Symbol(), PERIOD_CURRENT, m_rsiPeriod, PRICE_CLOSE);
        if(m_rsiHandle == INVALID_HANDLE) return false;
        
        m_bbHandle = iBands(Symbol(), PERIOD_CURRENT, m_bbPeriod, 0, 2.0, PRICE_CLOSE);
        if(m_bbHandle == INVALID_HANDLE) return false;
        
        m_adxHandle = iADX(Symbol(), PERIOD_CURRENT, m_adxPeriod);
        if(m_adxHandle == INVALID_HANDLE) return false;
        
        return true;
    }
    
    // 生成交易信号
    bool GenerateSignals(const double &high[], const double &low[], const double &close[], const double &volume[], int count) {
        if(count < m_maPeriod) return false;
        
        // 重置信号数组
        ArrayResize(m_signals, 0);
        m_signalCount = 0;
        
        // 获取指标数据
        double maBuffer[];
        double macdMainBuffer[];
        double macdSignalBuffer[];
        double rsiBuffer[];
        double bbUpperBuffer[];
        double bbLowerBuffer[];
        double adxBuffer[];
        double plusDI[];
        double minusDI[];
        
        ArraySetAsSeries(maBuffer, true);
        ArraySetAsSeries(macdMainBuffer, true);
        ArraySetAsSeries(macdSignalBuffer, true);
        ArraySetAsSeries(rsiBuffer, true);
        ArraySetAsSeries(bbUpperBuffer, true);
        ArraySetAsSeries(bbLowerBuffer, true);
        ArraySetAsSeries(adxBuffer, true);
        ArraySetAsSeries(plusDI, true);
        ArraySetAsSeries(minusDI, true);
        
        if(CopyBuffer(m_maHandle, 0, 0, count, maBuffer) != count) return false;
        if(CopyBuffer(m_macdHandle, 0, 0, count, macdMainBuffer) != count) return false;
        if(CopyBuffer(m_macdHandle, 1, 0, count, macdSignalBuffer) != count) return false;
        if(CopyBuffer(m_rsiHandle, 0, 0, count, rsiBuffer) != count) return false;
        if(CopyBuffer(m_bbHandle, 1, 0, count, bbUpperBuffer) != count) return false;
        if(CopyBuffer(m_bbHandle, 2, 0, count, bbLowerBuffer) != count) return false;
        if(CopyBuffer(m_adxHandle, 0, 0, count, adxBuffer) != count) return false;
        if(CopyBuffer(m_adxHandle, 1, 0, count, plusDI) != count) return false;
        if(CopyBuffer(m_adxHandle, 2, 0, count, minusDI) != count) return false;
        
        // 生成信号
        for(int idx = 1; idx < count; idx++) {
            // 检查MA信号
            CheckMASignal(maBuffer, close, idx);
            
            // 检查MACD信号
            CheckMACDSignal(macdMainBuffer, macdSignalBuffer, idx);
            
            // 检查RSI信号
            CheckRSISignal(rsiBuffer, idx);
            
            // 检查布林带信号
            CheckBBSignal(bbUpperBuffer, bbLowerBuffer, close, idx);
            
            // 检查ADX信号
            CheckADXSignal(adxBuffer, plusDI, minusDI, idx);
            
            // 检查成交量信号
            CheckVolumeSignal(volume, idx);
        }
        
        return m_signalCount > 0;
    }
    
    // 检查MA信号
    void CheckMASignal(const double &maBuffer[], const double &close[], int barIndex) {
        // 检查MA交叉
        bool maCrossUp = maBuffer[barIndex] > close[barIndex] && maBuffer[barIndex + 1] <= close[barIndex + 1];
        bool maCrossDown = maBuffer[barIndex] < close[barIndex] && maBuffer[barIndex + 1] >= close[barIndex + 1];
        
        if(maCrossUp) {
            AddSignal(SIGNAL_BUY, SOURCE_MA, 0.5, TimeCurrent(), close[barIndex]);
        } else if(maCrossDown) {
            AddSignal(SIGNAL_SELL, SOURCE_MA, 0.5, TimeCurrent(), close[barIndex]);
        }
    }
    
    // 检查MACD信号
    void CheckMACDSignal(const double &macdMainBuffer[], const double &macdSignalBuffer[], int barIndex) {
        // 检查MACD交叉
        bool macdCrossUp = macdMainBuffer[barIndex] > macdSignalBuffer[barIndex] && 
                          macdMainBuffer[barIndex + 1] <= macdSignalBuffer[barIndex + 1];
        bool macdCrossDown = macdMainBuffer[barIndex] < macdSignalBuffer[barIndex] && 
                            macdMainBuffer[barIndex + 1] >= macdSignalBuffer[barIndex + 1];
        
        if(macdCrossUp) {
            AddSignal(SIGNAL_BUY, SOURCE_MACD, 0.6, TimeCurrent(), 0.0);
        } else if(macdCrossDown) {
            AddSignal(SIGNAL_SELL, SOURCE_MACD, 0.6, TimeCurrent(), 0.0);
        }
    }
    
    // 检查RSI信号
    void CheckRSISignal(const double &rsiBuffer[], int barIndex) {
        // 检查RSI超买超卖
        if(rsiBuffer[barIndex] < m_rsiOversold) {
            AddSignal(SIGNAL_BUY, SOURCE_RSI, 0.7, TimeCurrent(), 0.0);
        } else if(rsiBuffer[barIndex] > m_rsiOverbought) {
            AddSignal(SIGNAL_SELL, SOURCE_RSI, 0.7, TimeCurrent(), 0.0);
        }
    }
    
    // 检查布林带信号
    void CheckBBSignal(const double &bbUpperBuffer[], const double &bbLowerBuffer[], 
                      const double &close[], int barIndex) {
        // 检查价格突破布林带
        if(close[barIndex] < bbLowerBuffer[barIndex]) {
            AddSignal(SIGNAL_BUY, SOURCE_BB, 0.8, TimeCurrent(), close[barIndex]);
        } else if(close[barIndex] > bbUpperBuffer[barIndex]) {
            AddSignal(SIGNAL_SELL, SOURCE_BB, 0.8, TimeCurrent(), close[barIndex]);
        }
    }
    
    // 检查ADX信号
    void CheckADXSignal(const double &adxBuffer[], const double &plusDI[], const double &minusDI[], int barIndex) {
        // 检查ADX趋势强度
        if(adxBuffer[barIndex] > m_adxThreshold) {
            if(plusDI[barIndex] > minusDI[barIndex]) {
                AddSignal(SIGNAL_BUY, SOURCE_ADX, 0.6, TimeCurrent(), 0.0);
            } else if(plusDI[barIndex] < minusDI[barIndex]) {
                AddSignal(SIGNAL_SELL, SOURCE_ADX, 0.6, TimeCurrent(), 0.0);
            }
        }
    }
    
    // 检查成交量信号
    void CheckVolumeSignal(const double &volume[], int barIndex) {
        // 检查成交量异常
        double avgVolume = 0.0;
        for(int idx = barIndex; idx < barIndex + 20; idx++) {
            avgVolume += volume[idx];
        }
        avgVolume /= 20.0;
        
        if(volume[barIndex] > avgVolume * m_volumeThreshold) {
            AddSignal(SIGNAL_BUY, SOURCE_VOLUME, 0.4, TimeCurrent(), 0.0);
        }
    }
    
    // 添加信号
    void AddSignal(ENUM_SIGNAL_TYPE type, ENUM_SIGNAL_SOURCE source, double strength, 
                  datetime time, double price) {
        Signal signal;
        signal.type = type;
        signal.source = source;
        signal.strength = strength;
        signal.time = time;
        signal.price = price;
        signal.isActive = true;
        
        ArrayResize(m_signals, m_signalCount + 1);
        m_signals[m_signalCount++] = signal;
    }
    
    // 获取信号数量
    int GetSignalCount() {
        return m_signalCount;
    }
    
    // 获取指定索引的信号
    bool GetSignal(int index, Signal &signal) {
        if(index < 0 || index >= m_signalCount) return false;
        
        signal = m_signals[index];
        return true;
    }
    
    // 获取最新信号
    bool GetLatestSignal(Signal &signal) {
        if(m_signalCount == 0) return false;
        
        signal = m_signals[m_signalCount - 1];
        return true;
    }
    
    // 清理过期信号
    void CleanupOldSignals(int maxAge) {
        for(int idx = m_signalCount - 1; idx >= 0; idx--) {
            if(!m_signals[idx].isActive && 
               (TimeCurrent() - m_signals[idx].time) > maxAge) {
                // 移除过期信号
                for(int jdx = idx; jdx < m_signalCount - 1; jdx++) {
                    m_signals[jdx] = m_signals[jdx + 1];
                }
                m_signalCount--;
            }
        }
    }
    
    // 验证信号强度
    bool ValidateSignalStrength(const Signal &signal) {
        return signal.strength >= 0.5; // 信号强度阈值
    }
    
    // 检查信号冲突
    bool HasSignalConflict(const Signal &signal) {
        for(int idx = 0; idx < m_signalCount; idx++) {
            if(m_signals[idx].isActive && 
               m_signals[idx].type != signal.type &&
               (TimeCurrent() - m_signals[idx].time) < PeriodSeconds(PERIOD_M15)) {
                return true;
            }
        }
        return false;
    }
}; 